<?php

class Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Sheet
{
    protected $_sheet = null;
    
    protected $_sharedStrings = null;
    
    public function __construct($sheet, $sharedStrings)
    {
        $this->_sheet = $sheet;
        $this->_sharedStrings = $sharedStrings;
    }
    
    public function toXml()
    {
        $xml = new XMLWriter();
        $xml->openMemory();
        
        $xml->startDocument('1.0', 'UTF-8', 'yes');
            $xml->startElement('worksheet');
                $xml->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
                $xml->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
                $xml->startElement('dimension');
                    $xml->writeAttribute('ref', 'A1');
                $xml->endElement(); // dimension
                $xml->startElement('sheetViews');
                    $xml->startElement('sheetView');
                        $xml->writeAttribute('tabSelected', '1');
                        $xml->writeAttribute('workbookViewId', '0');
                    $xml->endElement(); // sheetView
                $xml->endElement(); // sheetViews
                $xml->startElement('sheetFormatPr');
                    $xml->writeAttribute('defaultRowHeight', '15');
                $xml->endElement(); // sheetFormatPr
                $xml->startElement('sheetData');
                    
                    foreach ($this->_sheet->rows as $rowNum => $row) {
                        $xml->startElement('row');
                            $xml->writeAttribute('r', $rowNum + 1);
                            $xml->writeAttribute('spans', '1:' . count($row->cells));
                            
                            foreach ($row->cells as $cellNum => $cell) {
                                $xml->startElement('c');
                                    $xml->writeAttribute('r', chr(65 + $cellNum) . ($rowNum + 1));
                                    
                                    $value = $cell->getDisplayValue();
                                    if (is_numeric($value)) {
                                        $xml->writeElement('v', $value);
                                    } else {
                                        $xml->writeAttribute('t', 's');
                                        $xml->writeElement('v', $this->_sharedStrings->getStringIndex($value));
                                    }
                                    
                                $xml->endElement(); // c
                            }
                            
                        $xml->endElement(); // row
                    }
                    
                $xml->endElement(); // sheetData
                $xml->startElement('pageMargins');
                    $xml->writeAttribute('left', '0.7');
                    $xml->writeAttribute('right', '0.7');
                    $xml->writeAttribute('top', '0.75');
                    $xml->writeAttribute('bottom', '0.75');
                    $xml->writeAttribute('header', '0.3');
                    $xml->writeAttribute('footer', '0.3');
                $xml->endElement(); //pageMargins
            $xml->endElement(); // worksheet
        $xml->endDocument();
        
        return $xml->outputMemory();
    }
}
